package me.zhengjie.modules.region.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.region.domain.vo.ProvinceRegionVo;
import me.zhengjie.modules.region.service.SysRegionService;
import me.zhengjie.modules.region.service.dto.SysRegionDto;
import me.zhengjie.modules.region.service.dto.SysRegionQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.List;
import java.util.Set;

/**
* @author TianI
* @date 2022-01-27
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "区域接口管理")
@RequestMapping("/api/sysRegion")
public class SysRegionController {

    private final SysRegionService sysRegionService;

    @GetMapping
    @Log("查询区域接口")
    @ApiOperation("查询区域接口")
    @PreAuthorize("@el.check('sysRegion:list')")
    public ResponseEntity query(SysRegionQueryParam query, Pageable pageable){
        return new ResponseEntity<>(sysRegionService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增区域接口")
    @ApiOperation("新增区域接口")
    @PreAuthorize("@el.check('sysRegion:add')")
    public ResponseEntity create(@Validated @RequestBody SysRegionDto resources){
        return new ResponseEntity<>(sysRegionService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改区域接口")
    @ApiOperation("修改区域接口")
    @PreAuthorize("@el.check('sysRegion:edit')")
    public ResponseEntity update(@Validated @RequestBody SysRegionDto resources){
        sysRegionService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除区域接口")
    @ApiOperation("删除区域接口")
    @PreAuthorize("@el.check('sysRegion:del')")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        sysRegionService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @GetMapping("getRegionCascader")
    public ResponseEntity getRegionCascader(){
        List<ProvinceRegionVo> getregion = sysRegionService.getregion("100000");
        return new ResponseEntity<>(getregion,HttpStatus.OK);
    }

    /*
    @Log("导出区域接口")
    @ApiOperation("导出区域接口")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sysRegion:list')")
    public void download(HttpServletResponse response, SysRegionQueryParam query) throws IOException {
        sysRegionService.download(sysRegionService.queryAll(query), response);
    }*/

}
